<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <a-form :label-col="{ md: { span: 6 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }">
        <a-row>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="名称">
              <a-input v-model:value.trim="where.name" placeholder="请输入" @keyup.enter="reload" allow-clear />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="选择">
              <a-select v-model:value="where.sex" placeholder="请选择" @change="reload" allow-clear>
                <a-select-option value="1">1</a-select-option>
                <a-select-option value="2">2</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{ span: 24 }">
              <em></em>
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表格 -->
      <ele-pro-table ref="tableRef" row-key="id" :columns="columns" :datasource="datasource" :scroll="{ x: 1000 }">
        <template #toolbar>
          <a-space>
            <a-button type="primary" @click="openEdit()" v-any-permission="['order:customerFactory:save']">
              <template #icon>
                <plus-outlined />
              </template>
              <span>新建</span>
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="openEdit(record)" v-any-permission="['order:customerFactory:update']">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此项吗？" @confirm="remove(record)">
                <a class="ele-text-danger" v-any-permission="['order:customerFactory:remove']">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 工厂地址添加、修改弹窗 -->
    <a-modal :width="460" v-model:visible="visible" :confirm-loading="loading" :title="isUpdate ? '修改工厂地址' : '新建工厂地址'"
      :body-style="{ paddingBottom: '8px' }" @ok="save" :maskClosable="false">
      <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ md: { span: 7 }, sm: { span: 24 } }"
        :wrapper-col="{ md: { span: 17 }, sm: { span: 24 } }">
        <a-form-item label="公司id" name="organizationId">
          <a-input allow-clear :maxlength="200" placeholder="请输入公司id" v-model:value="form.organizationId" autocomplete="off" />
        </a-form-item>
        <a-form-item label="客户id" name="customerId">
          <a-input allow-clear :maxlength="200" placeholder="请输入客户id" v-model:value="form.customerId" autocomplete="off" />
        </a-form-item>
        <a-form-item label="地址" name="faddress">
          <a-input allow-clear :maxlength="200" placeholder="请输入地址" v-model:value="form.faddress" autocomplete="off" />
        </a-form-item>
        <a-form-item label="费用名,标准 转拼音" name="pinyin">
          <a-input allow-clear :maxlength="200" placeholder="请输入费用名,标准 转拼音" v-model:value="form.pinyin" autocomplete="off" />
        </a-form-item>
        <a-form-item label="默认地址 默认地址" name="fstate">
          <a-input allow-clear :maxlength="200" placeholder="请输入默认地址 默认地址" v-model:value="form.fstate" autocomplete="off" />
        </a-form-item>
        <a-form-item label="经度" name="longitude">
          <a-input allow-clear :maxlength="200" placeholder="请输入经度" v-model:value="form.longitude" autocomplete="off" />
        </a-form-item>
        <a-form-item label="纬度" name="latitude">
          <a-input allow-clear :maxlength="200" placeholder="请输入纬度" v-model:value="form.latitude" autocomplete="off" />
        </a-form-item>
        <a-form-item label="半径 默认米" name="radius">
          <a-input allow-clear :maxlength="200" placeholder="请输入半径 默认米" v-model:value="form.radius" autocomplete="off" />
        </a-form-item>
        <a-form-item label="备注" name="fremark">
          <a-input allow-clear :maxlength="200" placeholder="请输入备注" v-model:value="form.fremark" autocomplete="off" />
        </a-form-item>
        <a-form-item label="创建时间" name="createTime">
          <a-input allow-clear :maxlength="200" placeholder="请输入创建时间" v-model:value="form.createTime" autocomplete="off" />
        </a-form-item>
        <a-form-item label="创建人" name="createUser">
          <a-input allow-clear :maxlength="200" placeholder="请输入创建人" v-model:value="form.createUser" autocomplete="off" />
        </a-form-item>
        <a-form-item label="更新时间" name="updateTime">
          <a-input allow-clear :maxlength="200" placeholder="请输入更新时间" v-model:value="form.updateTime" autocomplete="off" />
        </a-form-item>
        <a-form-item label="更新人" name="updateUser">
          <a-input allow-clear :maxlength="200" placeholder="请输入更新人" v-model:value="form.updateUser" autocomplete="off" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { toDateString } from 'ele-admin-pro';
import useFormData from '@/utils/use-form-data';
import { message } from 'ant-design-vue';

import { pageCustomerFactorys, removeCustomerFactory, addCustomerFactory, updateCustomerFactory } from '@/api/order/customerFactory';

// 表格实例
const tableRef = ref(null);

// 表格列配置
const columns = ref([
  {
    key: 'index',
    width: 48,
    align: 'center',
    fixed: 'left',
    hideInSetting: true,
    customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
  },
  {
    title: '地址',
    dataIndex: 'faddress',
    key: 'faddress',
    // sorter: true,
  },
  {
    title: '默认地址 默认地址',
    dataIndex: 'fstate',
    key: 'fstate',
    // sorter: true,
  },
  {
    title: '经度',
    dataIndex: 'longitude',
    key: 'longitude',
    // sorter: true,
  },
  {
    title: '纬度',
    dataIndex: 'latitude',
    key: 'latitude',
    // sorter: true,
  },
  {
    title: '备注',
    dataIndex: 'fremark',
    key: 'fremark',
    // sorter: true,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    // sorter: true,
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '创建人',
    dataIndex: 'createName',
    key: 'createName',
    // sorter: true,
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
    // sorter: true,
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '更新人',
    dataIndex: 'updateName',
    key: 'updateName',
    // sorter: true,
  },
  {
    title: '操作',
    key: 'action',
    width: 200,
    align: 'center',
    hideInSetting: true
  }
]);

// 表单数据
const { form: where, resetFields } = useFormData({
  username: '',
  sex: undefined
});

// 表格数据源
const datasource = ({ page, limit, where, orders }) => {
  return pageCustomerFactorys({ ...where, ...orders, page, limit });
};

/* 搜索 */
const reload = () => {
  tableRef?.value?.reload({ page: 1, where: where });
};

/*  重置 */
const reset = () => {
  resetFields();
  reload();
};

/* 删除单个 */
const remove = (record) => {
  const hide = message.loading('请求中..', 0);
  removeCustomerFactory(record.id).then((msg) => {
    hide();
    message.success(msg);
    reload();
  }).catch((e) => {
    hide();
    message.error(e.message);
  });
};

// 当前选中数据
const current = ref(null);

// 弹窗是否打开
const visible = ref(false);

// 是否是修改
const isUpdate = ref(false);

// 提交状态
const loading = ref(false);

//
const formRef = ref(null);

// 表单数据
const { form, resetFields: resetFormFields } = useFormData({
  id: undefined,
  customer: '',
  vessel: '',
});

// 表单验证规则
const rules = reactive({
  name: [
    {
      required: true,
      message: '请输入名称',
      type: 'string',
      trigger: 'blur'
    }
  ],
});

/* 打开编辑弹窗 */
const openEdit = (record) => {
  resetFormFields();
  formRef.value?.clearValidate();
  current.value = record?? {};
  visible.value = true;
  Object.assign(form, record ?? {});
  isUpdate.value = !!record;
};

/* 保存编辑 */
const save = () => {
  if (!formRef.value) {
    return;
  }
  formRef.value.validate().then(() => {
    loading.value = true;
    const saveOrUpdate = isUpdate.value ? updateCustomerFactory : addCustomerFactory;
    saveOrUpdate(form).then((msg) => {
      loading.value = false;
      message.success(msg);
      visible.value = false;
      reload();
    }).catch((e) => {
      loading.value = false;
      message.error(e.message);
    });
  }).catch(() => {
  });
};
</script>

<script>
export default {
  name: 'DemoTest'
};
</script>

